﻿using System;
using System.Collections.Generic;
using System.Net;
using System.Net.Http;
using System.Web.Http;
using ThumpThumpBus.Models.Repository;
using ThumpThumpBus.Models.ViewModels;

namespace ThumpThumpBus.Controllers
{
    public class PeopleServiceController : ApiController
    {
        private readonly PersonRepository _personRepository = new PersonRepository();
        private readonly IncidentRepository _incidentRepository = new IncidentRepository();

        public IEnumerable<PersonViewModel> Get()
        {
            var viewData = new List<PersonViewModel>();

            var people = _personRepository.GetAllPeople();
            foreach (var p in people)
            {
                var person = new PersonViewModel
                {
                    PersonId = p.id,
                    PersonName = p.name,
                    ThrowCount = _incidentRepository.GetThrowCountByPerson(p),
                    HitCount = _incidentRepository.GetHitCountByPerson(p)
                };
                viewData.Add(person);
            }

            return viewData;
        }

        public PersonViewModel Get(int id)
        {
            var person = _personRepository.GetPersonById(id);
            if (person == null) throw new HttpResponseException(
                new HttpResponseMessage(HttpStatusCode.NotFound));

            return new PersonViewModel
            {
                PersonId = person.id,
                PersonName = person.name,
                ThrowCount = _incidentRepository.GetThrowCountByPerson(person),
                HitCount = _incidentRepository.GetHitCountByPerson(person)
            };
        }
    }
}
 